<!--
 * @Descripttion: (工艺流程/Lm_Route)
 * @Author: (admin)
 * @Date: (2024-10-22)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item label="RouteId" prop="routeId">
        <el-input v-model="queryParams.routeId" placeholder="请输入RouteId" />
      </el-form-item>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['lmroute:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table :data="dataList" v-loading="loading" ref="table" border
      header-cell-class-name="el-table-header-cell" highlight-current-row @sort-change="sortChange">
      <el-table-column align="center" width="90">
        <template #default="scope">
          <el-button text @click="rowClick(scope.row)">{{ $t('btn.details') }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="routeId" label="编号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('routeId')"/>
      <el-table-column prop="routeName" label="名称" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('routeName')"/>
      <el-table-column prop="description" label="描述" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('description')"/>
      <el-table-column prop="createBy" label="创建用户" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('createBy')"/>
      <el-table-column prop="createTime" label="创建时间" :show-overflow-tooltip="true"  v-if="columns.showColumn('createTime')"/>
      <el-table-column prop="updateBy" label="更新用户" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('updateBy')"/>
      <el-table-column prop="updateTime" label="更新时间" :show-overflow-tooltip="true"  v-if="columns.showColumn('updateTime')"/>
      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button type="success" size="small" icon="edit" title="编辑" v-hasPermi="['lmroute:edit']" @click="handleUpdate(scope.row)"></el-button>
          <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['lmroute:delete']" @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    <!-- 详细区域 -->
    <el-drawer v-model="drawer" size="80%" direction="rtl">
      <el-table :data="lmRouteitemList" header-row-class-name="text-navy">
        <el-table-column label="序号" type="index" width="80" />
        <el-table-column prop="routeId" label="RouteId"/>
        <el-table-column prop="processId" label="ProcessId"/>
        <el-table-column prop="times" label="Times"/>
        <el-table-column prop="processType" label="ProcessType">
          <template #default="scope">
            <dict-tag :options=" options.processTypeOptions" :value="scope.row.processType"  />
          </template>
        </el-table-column>
        <el-table-column prop="operate" label="Operate"/>
        <el-table-column prop="testNum" label="TestNum"/>
        <el-table-column prop="checkType" label="CheckType">
          <template #default="scope">
            <dict-tag :options=" options.checkTypeOptions" :value="scope.row.checkType"  />
          </template>
        </el-table-column>
        <el-table-column prop="spotCheck" label="SpotCheck"/>
        <el-table-column prop="material" label="Material"/>
        <el-table-column prop="deviceID" label="DeviceID"/>
        <el-table-column prop="createBy" label="CreateBy"/>
        <el-table-column prop="createTime" label="CreateTime"/>
        <el-table-column prop="updateBy" label="UpdateBy"/>
        <el-table-column prop="updateTime" label="UpdateTime"/>
      </el-table>
    </el-drawer>
    <!-- 添加工艺流程区域 -->
    <el-dialog :title="title" :lock-scroll="false" v-model="open" :fullscreen="fullScreen" width="1000" draggable>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="150px">
        <el-row :gutter="20">
          <el-col :lg="12">
            <el-form-item label="RouteId" prop="routeId">
              <el-input v-model="form.routeId" placeholder="请输入RouteId" :disabled="opertype != 1"/>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="RouteName" prop="routeName">
              <el-input v-model="form.routeName" placeholder="请输入RouteName" />
            </el-form-item>
          </el-col>
          <el-col :lg="24">
            <el-form-item label="Description" prop="description">
              <el-input v-model="form.description" placeholder="请输入Description" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="center">工艺流程明细信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddLmRouteitem">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteLmRouteitem">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="info" icon="FullScreen" @click="fullScreen = !fullScreen">{{ fullScreen ? '退出全屏' : '全屏' }}</el-button>
          </el-col>
        </el-row>
        <el-table :data="lmRouteitemList" :row-class-name="rowLmRouteitemIndex" @selection-change="handleLmRouteitemSelectionChange" ref="LmRouteitemRef">
          <el-table-column type="selection" width="40" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="Times" align="center" prop="times">
            <template #default="scope">
              <el-input v-model="scope.row.times" placeholder="请输入Times" />
            </template>
          </el-table-column>
          <el-table-column label="ProcessType" prop="processType">
            <template #default="scope">
              <el-select v-model="scope.row.processType" placeholder="请选择ProcessType">
                <el-option
                  v-for="item in options.processTypeOptions" 
                  :key="item.dictValue" 
                  :label="item.dictLabel" 
                  :value="item.dictValue"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="Operate" align="center" prop="operate">
            <template #default="scope">
              <el-input v-model="scope.row.operate" placeholder="请输入Operate" />
            </template>
          </el-table-column>
          <el-table-column label="TestNum" align="center" prop="testNum">
            <template #default="scope">
              <el-input v-model="scope.row.testNum" placeholder="请输入TestNum" />
            </template>
          </el-table-column>
          <el-table-column label="CheckType" prop="checkType">
            <template #default="scope">
              <el-select v-model="scope.row.checkType" placeholder="请选择CheckType">
                <el-option
                  v-for="item in options.checkTypeOptions" 
                  :key="item.dictValue" 
                  :label="item.dictLabel" 
                  :value="item.dictValue"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="SpotCheck" align="center" prop="spotCheck">
            <template #default="scope">
              <el-input v-model="scope.row.spotCheck" placeholder="请输入SpotCheck" />
            </template>
          </el-table-column>
          <el-table-column label="Material" align="center" prop="material">
            <template #default="scope">
              <el-input v-model="scope.row.material" placeholder="请输入Material" />
            </template>
          </el-table-column>
          <el-table-column label="DeviceID" align="center" prop="deviceID">
            <template #default="scope">
              <el-input v-model="scope.row.deviceID" placeholder="请输入DeviceID" />
            </template>
          </el-table-column>
          <el-table-column label="CreateBy" align="center" prop="createBy">
            <template #default="scope">
              <el-input v-model="scope.row.createBy" placeholder="请输入CreateBy" />
            </template>
          </el-table-column>
          <el-table-column label="CreateTime" align="center" prop="createTime">
            <template #default="scope">
              <el-date-picker clearable v-model="scope.row.createTime" type="date" placeholder="选择日期时间"></el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="UpdateBy" align="center" prop="updateBy">
            <template #default="scope">
              <el-input v-model="scope.row.updateBy" placeholder="请输入UpdateBy" />
            </template>
          </el-table-column>
          <el-table-column label="UpdateTime" align="center" prop="updateTime">
            <template #default="scope">
              <el-date-picker clearable v-model="scope.row.updateTime" type="date" placeholder="选择日期时间"></el-date-picker>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="lmroute">
import { listLmRoute, addLmRoute, delLmRoute, updateLmRoute,getLmRoute, } from '@/api/craft/lmroute.js'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: 'RouteId',
  sortType: 'asc',
  routeId: undefined,
})
const columns = ref([
  { visible: true, align: 'center', type: '', prop: 'routeId', label: 'RouteId'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'routeName', label: 'RouteName'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'description', label: 'Description'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'createBy', label: 'CreateBy'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'createTime', label: 'CreateTime'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'updateBy', label: 'UpdateBy'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'updateTime', label: 'UpdateTime'  ,showOverflowTooltip: true  },
  //{ visible: false, prop: 'actions', label: '操作', type: 'slot', width: '160' }
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])

var dictParams = []

function getList(){
  loading.value = true
  listLmRoute(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery(){
  proxy.resetForm("queryRef")
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
    routeId: [{ required: true, message: "RouteId不能为空", trigger: "blur"     }],
    routeName: [{ required: true, message: "RouteName不能为空", trigger: "blur"     }],
  },
  options: {
    // ProcessType 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
processTypeOptions: [],
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel(){
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    routeId: null,
    routeName: null,
    description: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
  };
  lmRouteitemList.value = []
  proxy.resetForm("formRef")
}

// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = '添加工艺流程'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.routeId || ids.value
  getLmRoute(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '修改工艺流程'
      opertype.value = 2

      form.value = {
        ...data,
      }
      lmRouteitemList.value = res.data.lmRouteitemNav
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      form.value.lmRouteitemNav = lmRouteitemList.value
      if (form.value.routeId != undefined && opertype.value === 2) {
        updateLmRoute(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addLmRoute(form.value).then((res) => {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getList()
          })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.routeId || ids.value
  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', "警告", {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delLmRoute(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
}

/*********************工艺流程明细子表信息*************************/
const lmRouteitemList = ref([])
const checkedLmRouteitem = ref([])
const fullScreen = ref(false)
const drawer = ref(false)

/** 工艺流程明细序号 */
function rowLmRouteitemIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 工艺流程明细添加按钮操作 */
function handleAddLmRouteitem() {
  let obj = {};
  //下面的代码自己设置默认值
  //obj.times = null;
  //obj.processType = null;
  //obj.operate = null;
  //obj.testNum = null;
  //obj.checkType = null;
  //obj.spotCheck = null;
  //obj.material = null;
  //obj.deviceID = null;
  //obj.createBy = null;
  //obj.createTime = null;
  //obj.updateBy = null;
  //obj.updateTime = null;
  lmRouteitemList.value.push(obj);
}

/** 复选框选中数据 */
function handleLmRouteitemSelectionChange(selection) {
  checkedLmRouteitem.value = selection.map(item => item.index)
}

/** 工艺流程明细删除按钮操作 */
function handleDeleteLmRouteitem() {
  if(checkedLmRouteitem.value.length == 0){
    proxy.$modal.msgError('请先选择要删除的工艺流程明细数据')
  } else {
    const LmRouteitems = lmRouteitemList.value;
    const checkedLmRouteitems = checkedLmRouteitem.value;
    lmRouteitemList.value = LmRouteitems.filter(function(item) {
      return checkedLmRouteitems.indexOf(item.index) == -1
    });
  }
}

/** 工艺流程明细详情 */
function rowClick(row) {
  const id = row.routeId || ids.value
  getLmRoute(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      drawer.value = true
      lmRouteitemList.value = data.lmRouteitemNav
    }
  })
}
handleQuery()
</script>